{include file="public/header" /}
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="ibox float-e-margins">
        <div class="ibox-title">
            <h5>菜单列表</h5>
            <div class="ibox-tools">
                <a class="collapse-link">
                    <i class="fa fa-chevron-up"></i>
                </a>
            </div>
        </div>
        <div class="ibox-content">          
            <div class="row">
                <div class="col-sm-12">   
                    <div  class="col-sm-3">
                        <div class="input-group" >
                            <a href="{:url('add_rule')}">
                                <button type="button" class="btn btn-primary btn-outline"  >
                                    <i class="fa fa-plus"></i> 添加菜单
                                </button>
                            </a>
                            <button  class="btn btn-outline btn-danger" href="javascript:;" data-toggle="dropdown">
                                <i class="fa fa-wrench"></i> 批量操作 <span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu animated fadeInDown" role="menu">
                                <li><a href="javascript:;" class="layuiBtn" id="empty" data-type="reload"><i class="fa fa-retweet"></i> 重置搜索框 </a></li>
                                <li><a href="javascript:;" class="layuiBtn" data-type="getCheckData"><i class="fa fa-trash-o"></i> 批量删除 </a></li>
                            </ul>
                        </div>
                    </div>
                    <div class="col-sm-9">
                        <form id="layuiForm">
                            <div class="col-sm-5">
                                <div class="input-group" >
                                    <input type="text"  name="start" onclick="laydate({istime: true,format: 'YYYY-MM-DD hh:mm'})" class="form-control layer-date search" placeholder="开始日期"/>
                                    <span class="input-group-addon">到</span>
                                    <input type="text"  name="end" onclick="laydate({istime: true,format: 'YYYY-MM-DD hh:mm'})"  class="form-control layer-date search" placeholder="结束日期"/>
                                </div>
                            </div>
                            <div class="col-sm-4">
                                <div class="input-group">
                                    <input type="text" id="key" class="form-control search" name="key" value="" placeholder="输入需查询的权限名称" />
                                    <span class="input-group-btn">
                                        <a  class="btn btn-primary layuiBtn" data-type="reload"><i class="fa fa-search"></i> 搜索</a>
                                    </span>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div class="hr-line-dashed"></div>
            <div class="example-wrap">
                <div class="example">
                    <table class="layui-hide" id="test" lay-filter="testReload"></table>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/footer" /}
<script>
    layui.use('table', function () {
        var table = layui.table
            , form = layui.form;
        table.render({
            elem: '#test'
            , url: '{:url("Menu/index")}'
            , page: true
            , even: false //开启隔行背景
            , size: 'lg' //小尺寸的表格
            // ,width : 1000
            // ,height: 'full-20'
            , cellMinWidth: 80
            , limits: []
            , limit: 1000
            , text: {
                none: '暂无数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            }
            , loading: true
            , id: 'testReload'
            , cols: [[
                {type: 'checkbox', fixed: 'left',}
                , {field: 'id', width: 60, title: 'ID',  align: 'center'}
                , {field: 'title', width: '' ,title: '权限名称', align: 'left',edit: 'text'}
                , {field: 'name', width: '', title: '节点', align: 'center',edit: 'text'}
                , {field: 'create_time', width: '', title: '添加时间', align: 'center'}
                , {field: 'sort',fixed: 'right', width: 90, title: '排序', align: 'center',edit: 'text'}
                , {field: 'status',fixed: 'right', width: 100, title: '状态', align: 'center', templet: '#staBar'}
                , {fixed: 'right', width: 250, title: '操作', align: 'center', toolbar: '#opeBar'}
            ]],
            done: function (res, curr, count) {
                $('th').children().prop('align','center');
            }
        });
        //监听状态开关操作
        form.on('switch(OnOff)', function (obj) {
            // layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
            //菜单状态
            if(obj.elem.checked == true){
                var num = 1;
            }else{
                var num = 2;
            }
            wk.status(this.value,num, '{:url("rule_state")}');
        });
        //监听单元格编辑
        table.on('edit(testReload)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            // layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
            if(field == 'sort'){
                var patrn = /^[0-9]*$/;
                if(!patrn.test(value)){
                    layer.msg('排序值只能为数字',{icon:2,time:1500,shade: 0.1},function(){
                        //重载当前页表格
                        $(".layui-laypage-btn").click();
                    });
                    return false;
                }
                wk.change(data.id,"{:url('ruleorder')}",field,value);
            }else{
                wk.change(data.id,"{:url('editField')}",field,value);
            }
        });
        //搜索表格重载
        var $ = layui.$, active = {
            reload: function () {
                //执行重载
                table.reload('testReload', {
                   where:$('#layuiForm').parseForm()
                });
            }
            ,getCheckData: function(){
                //批量删除
                layer.confirm('选中菜单的子分类将同时删除,确认删除吗?', {icon: 7, title:'警告'}, function(index){
                    $.ajax({
                        url: "{:url('batchDelMenu')}",
                        type: "post",
                        dataType: "json",
                        data: 'ids='+getIds(),
                        success: function (res) {
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1, time: 1500, shade: 0.1},function(index){
                                    layer.close(index);
                                    if (res.data != "") {
                                        var num = res.data.split(',');
                                        $.each(num, function (index, item) {
                                            $('#del_' + item).parents("tr").remove();
                                        })
                                    }
                                });
                            } else if (res.code == 100) {
                                wk.error(res.msg,2,'');
                            }
                        }
                    });
                    layer.close(index);
                })
            }
        };
        $('.layuiBtn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        var getIds = function () {
            var ids = [];
            var checkStatus = table.checkStatus('testReload')
                ,data = checkStatus.data;
            $.each(data,function(index,item){
                ids.push(item['id'])
            });
            return ids;
        }
    });
</script>
<!--状态模板-->
<script type="text/html" id="staBar">
    <input type="hidden" id="zh{{d.id}}" value="{{d.status}}">
    <input type="checkbox"  id="zt{{d.id}}" value="{{d.id}}"  lay-skin="switch"  lay-text="ON|OFF" lay-filter="OnOff"{{ d.status == 1 ? 'checked' : '' }}  >
</script>
<!--操作模板-->
<script type="text/html" id="opeBar">
    <a href="{:url('add_rule')}?id={{d.id}}" class="btn btn-info btn-outline btn-xs">
        <i class="fa fa-plus"></i> 添加子菜单
    </a>&nbsp;&nbsp;
    <a href="javascript:location.href = '{:url('edit_rule')}?id={{d.id}}';" class="btn btn-primary btn-outline btn-xs">
        <i class="fa fa-paste"></i> 编辑
    </a>&nbsp;&nbsp;
    <a href="javascript:del_menu(this,'{{d.id}}');" id="del_{{d.id}}" class="btn btn-danger btn-outline btn-xs">
        <i class="fa fa-trash-o"></i> 删除
    </a>
</script>
<script type="text/javascript">
    /**
     * [del_menu 删除菜单]
     * @Author
     * @param   {[type]}    id [用户id]
     */
    function del_menu(obj,id){
        // wk.confirm(id,'{:url("del_rule")}');
        layer.confirm('此菜单的子分类将同时删除,确认删除吗?', {icon: 7, title:'警告'}, function(index){
            //do something
            $.getJSON('./del_menu', {'id' : id}, function(res){
                if(res.code == 200){
                    layer.msg(res.msg,{icon:1,time:1500,shade: 0.1},function(index){
                        layer.close(index);
                        var num = res.data.split(',');
                        $.each(num, function (index, item) {
                            $('#del_' + item).parents("tr").remove();
                        })
                    });
                }else if(res.code == 100){
                    wk.error(res.msg,2,'');
                }
            });

            layer.close(index);
        })
    }
</script>
</body>
</html>